
$(function (){
    Date.prototype.format = function(format)
    {
        var o = {
            "M+" : this.getMonth()+1, //month
            "d+" : this.getDate(),    //day
            "h+" : this.getHours(),   //hour
            "m+" : this.getMinutes(), //minute
            "s+" : this.getSeconds(), //second
            "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
            "S" : this.getMilliseconds() //millisecond
        }
        if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
            (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)if(new RegExp("("+ k +")").test(format))
            format = format.replace(RegExp.$1,
                RegExp.$1.length==1 ? o[k] :
                    ("00"+ o[k]).substr((""+ o[k]).length));
        return format;
    }
    //微信用户登录，强制登录型
    login()
    //微信验证登录并设置分享
    wxSDK()
        .then(res=>{

            SDKyanzheng(res).then(res=>{

                let title = document.title
                let url = location.href
                let imgUrl=$('#zhandianIcon').attr('href')
                let desc='我的24 现场24 CH24'
                fenxiang(title,url,imgUrl,desc)

            })

        }).catch(error=>{
        console.log(error)
    })
    const openid = 'oW2Wk1QWK-W6MlrvBG7qSP6CUshk'//lsa.get("openid")
    const db = wxCloud.db()
    const flow = layui.flow;
    init()

    async function init(){

        reloadFlow()

        let res = await db.collection('sgb-login-user-info').where({openid}).get()

        let userinfo = res.data[0]
        console.log(userinfo);

        $("#headerImg").attr('src',userinfo.headimgurl)
        $("#nickName_a").text(userinfo.nickname)


        db.collection('zanmeifangwen')
        .orderBy('date','desc')
        .limit(1)
        .watch({
            onChange: function(snapshot) {
                console.log('snapshot', snapshot)
            },
            onError: function(err) {
                console.error('the watch closed because of error', err)
            }
        })

    }
    /**
     * 建议列表初始化和重载的函数
     * @parms selectAll{布尔值}
     */
    function reloadFlow(){

        $('#list').html('')
        flow.load({
             elem: '#list' //指定列表容器
            ,done: async function(page, next){ //到达临界点（默认滚动触发），触发下一页
                console.log(123)
                let lis = []
                let skip = (page-1)*10
                let count = await db.collection('zanmeifangwen').where({openId:openid}).count()
                let total = count.total
                db.collection('zanmeifangwen').where({openId:openid}).orderBy('date','desc').get()
                .then(res=>{
                    console.log(res);
                    let list = res.data
                    let pagetotal = Math.floor(total / 10)
                    console.log('pagetotal=>',pagetotal)
                    layui.each(list, function(index, item){
                        // console.log(item);
                        let date = new Date(item.time)
                        $lis = `<li class="list-group-item">
                                    <span>${item.mulu}</span>
                                    <span>${new Date(item.date).format('yyyy-MM-dd hh:mm:ss')}</span>
                                </li>`
                        lis.push($lis);
                    });

                    next(lis.join(""), page <= pagetotal );

                })
            }
        });

    }



})

